<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>菜单</title>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>

</head>
<body>
<div id="app">
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">

        <el-form-item label="当前状态" >
            <el-select v-model="searchForm.status" placeholder="请选择"     style="width:100px" >
                <el-option label="已上架" value="1"></el-option>
                <el-option label="未上架" value="0"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="菜品名称">
            <el-input v-model="searchForm.goodsName" placeholder="菜品名称"></el-input>
        </el-form-item>
        <el-form-item label="菜品分类">
            <el-input v-model="searchForm.categoryId" placeholder="菜品分类"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
    </el-form>

    <el-row>
        <el-button type="danger" @click="deleteByIds">删除</el-button>
        <el-button type="primary" @click="addGoods">新增</el-button>
        <el-button type="success" @click="update1">上传菜单</el-button>
    </el-row>
    <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="30%"
            :before-close="handleClose">
        <el-form ref="form" :model="Goods" label-width="80px">
            <el-form-item label="菜品名称">
                <el-input v-model="Goods.goodsName"></el-input>
            </el-form-item>
            <el-form-item label="菜品类别">
                <el-input v-model="Goods.categoryId"></el-input>
            </el-form-item>
            <el-form-item label="状态">
                <el-switch v-model="Goods.status"
                           active-value="1"
                           inactive-value="0"
                ></el-switch>
            </el-form-item>
            <el-form-item label="备注">
                <el-input type="textarea" v-model="Goods.description"></el-input>
            </el-form-item>
            <el-form-item label="价格">
                <el-input v-model="Goods.price"></el-input>
            </el-form-item>
            <el-form-item label="计量单位">
                <el-input v-model="Goods.unit"></el-input>
            </el-form-item>

                        <el-form-item label="图片" v-model="Goods.imgUrl">
                            <el-upload
                                    list-type="picture-card"
                                    action="/login/updateZpServlet"
                                    :on-change="onChange"
                                    :on-success="handleAvatarSuccess"
                            >
                                <i class="el-icon-plus"></i>
                            </el-upload>
                        </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submit">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
    </el-form-item>
    </el-form>

    </el-dialog>


    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>
            <el-table-column
                    prop="goodsName"
                    label="菜品名称"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="categoryId"
                    label="菜品类别"
                    align="center">
            </el-table-column>
            <el-table-column
                    prop="statusStr"
                    align="center"
                    label="当前状态">
            </el-table-column>
            <el-table-column
                    prop="description"
                    align="center"
                    label="菜的详细介绍">
            </el-table-column>
            <el-table-column
                    prop="price"
                    align="center"
                    label="价格">
            </el-table-column>
            <el-table-column
                    prop="unit"
                    align="center"
                    label="计量单位">
            </el-table-column>
            <el-table-column prop="imgUrl" label="图片" align="center">
                <template slot-scope="{ row }">
                    <el-image style="width: auto; height: 40px; border:none;cursor: pointer;"
                              :src="getImage(row.imgUrl)"
                              :preview-src-list="[ `downloadServlet?name=${row.imgUrl}` ]" >
                    </el-image>
                </template>
            </el-table-column>

            <el-table-column
                    prop="operation"
                    align="center"
                    label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            @click="updateGoods(scope.row)">编辑
                    </el-button>
                </template>
            </el-table-column>


        </el-table>
    </template>
    <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10,15 , 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
</div>
<script src="./js/vue.js"></script>
<script src="./element-ui/lib/index.js"></script>
<link rel="stylesheet" href="./element-ui/lib/theme-chalk/index.css">
<script src="./js/axios-0.18.0.js"></script>
<script>


    new Vue({
        el: "#app",
        mounted() {
            let _this =this;
            // 当页面加载完成后，发送异步请求，获取数据
            _this.selectAll();
        },
        methods: {
            // selectAll() {
            //     var _this = this;
            //     axios({
            //         method: "get",
            //         url: "http://localhost/login/selectAllServlet"
            //     }).then(function (resp) {
            //         _this.tableData = resp.data;
            //     })
            // },
            //分页查询
            selectAll() {
                let _this = this;
                axios({
                    method: "post",
                    url: "http://localhost:8080/login/selectByPageCondition?currentPage=" + _this.currentPage + "&pageSize=" + _this.pageSize,
                    data: this.Goods
                }).then(function (resp) {
                    _this.tableData = resp.data.rows;
                    _this.total = resp.data.totalCount;
                })
            },
            //每页显示条数发生变化
            handleSizeChange(val) {
                // console.log(val)
                this.pageSize = val;
                this.selectAll();
            },
            //当前页发生变化
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                this.currentPage = val;
                this.selectAll();
            },
            handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            handlePreview(file) {
                console.log(file);
            },
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;

                // console.log(this.multipleSelection)
            },
            // onSubmit() {
            //     //console.log(this.brand);
            //     this.selectAll();
            //
            // },
            onSubmit() {
                let _this = this;
                axios({
                    method: "post",
                    url: "http://localhost:8080/login/goods/selectByPageCondition?currentPage=" + _this.currentPage + "&pageSize=" + _this.pageSize,
                    data: this.searchForm
                }).then(function (resp) {
                    _this.tableData = resp.data.rows;
                    _this.total = resp.data.totalCount;
                })
            },
            update1() {
                this.$confirm('此操作将提交到菜单, 是否继续?', '提示', {
                    "confirmButtonText": '确定',
                    "cancelButtonText": '取消',
                    "type": 'warning'
                }).then(() => {
                    for (let i = 0; i < this.multipleSelection.length; i++) {
                        let selectElement = this.multipleSelection[i];
                        this.selectByIds[i] = selectElement.id;
                    }
                    let _this = this;
                    axios({
                        method: "post",
                        url: "http://localhost:8080/login/menuServlet",
                        data: _this.selectByIds
                    }).then(function (response) {
                        if (response.data == "success") {
                            //成功添加关闭对话窗
                            //    重新再查询
                            _this.selectAll();
                            //跳出信息
                            _this.$message({
                                "message": '恭喜你，添加菜品成功',
                                "type": 'success'
                            });
                        }
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            submit(){
                this.dialogVisible = false;
                if (this.dialogTitle == "addGoods") {
                    let _this = this;
                    axios({
                        method: "post",
                        url: "http://localhost:8080/login/addServlet",
                        data: _this.Goods,

                    }).then(function (response) {
                        if (response.data == "success") {
                            //成功添加关闭对话窗
                            _this.dialogVisible = false;
                            //    重新再查询
                            _this.selectAll();
                            //跳出信息
                            _this.$message({
                                message: '恭喜你，添加成功',
                                type: 'success'
                            });

                        }

                    })

                }
                if (this.dialogTitle == "updateGoods"){

                    let _this = this;
                    axios({
                        method: "post",
                        url: "http://localhost:8080/login/updateServlet",
                        data: _this.Goods,

                    }).then(function (response) {
                        if (response.data == "success") {
                            //成功添加关闭对话窗
                            _this.dialogVisible = false;
                            //    重新再查询
                            _this.selectAll();
                            //跳出信息
                            _this.$message({
                                message: '恭喜你，修改成功',
                                type: 'success'
                            });

                        }
                    })
                }
            },

            updateGoods(row){
                this.dialogVisible = true;
                this.dialogTitle = "updateGoods";
                this.Goods = row;

            },
            addGoods() {
                this.Goods = {};
                this.dialogVisible = true;
                this.dialogTitle = "addGoods";
            },
            //展现对话框
            showDialog() {
                this.dialogVisible = true;
                this.Goods = {
                    goodsName: '',
                    categoryId: '',
                    description: '',
                    price: '',
                    unit: '',
                    imgUrl: '',
                    id: ''
                }
            },

            getImage (imgUrl) {
                return `downloadServlet?name=${imgUrl}`
            },
            delBatch() {
                console.log('submit')
            },
            handleClose() {
                this.dialogVisible = false;
            },



            deleteByIds() {

                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    for (let i = 0; i < this.multipleSelection.length; i++) {
                        let selectElement = this.multipleSelection[i];
                        this.selectByIds[i] = selectElement.id;
                    }
                    var _this = this
                    axios({
                        method: "post",
                        url: "http://localhost:8080/login/goods/delete",
                        data: _this.selectByIds
                    }).then(function (response) {
                        if (response.data == "success") {
                            //成功添加关闭对话窗
                            //    重新再查询
                            _this.selectAll();
                            //跳出信息
                            _this.$message({
                                message: '恭喜你，删除成功',
                                type: 'success'
                            });
                        }
                    })

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },

            handleAvatarSuccess (response, file, fileList) {
                // 拼接down接口预览
                if(response.code === 0 && response.msg === '未登录'){
                    window.top.location.href = 'http://localhost:8080/login/login.html'
                }else{
                    alert(response.data)
                    this.imageUrl = `downloadServlet?name=${response.data}`
                    this.Goods.imgUrl = response.data
                }
            },
            //照片验证
            onChange(file) {
                if (file) {
                    const suffix = file.name.split('.')[1]
                    const size = file.size / 1024 / 1024 < 2
                    if (['png', 'jpeg', 'jpg'].indexOf(suffix) < 0) {
                        this.$message.error('上传图片只支持 png、jpeg、jpg 格式！')
                        this.$refs.upload.clearFiles()
                        return false
                    }
                    if (!size) {
                        this.$message.error('上传文件大小不能超过 2MB!')
                        return false
                    }
                    return file
                }
            },
        },

        data() {
            return {
                fileList: [{name: '', url: ''},],
                rands: [],
                msg: '',
                imageUrl: '',
                total: 400,
                selectByIds: [],
                currentPage: 1,
                pageSize: 5,
                multipleSelection: [],
                dialogVisible: false,
                titleMap : {
                    addGoods : "添加数据",
                    updateGoods : "修改数据"
                },
                dialogTitle : "",
                tableData: [{
                    goodsName: '华为',
                    categoryId: '华为',
                    status: "1",
                    description: '12',
                    price: '启用',
                    unit: '元',
                    imgUrl: ''
                }, {
                    goodsName: '华为',
                    categoryId: '华为',
                    status: "1",
                    description: '12',
                    price: '启用',
                    unit: '元',
                    imgUrl: ''
                }, {
                    goodsName: '华为',
                    categoryId: '华为',
                    status: "1",
                    description: '12',
                    price: '启用',
                    unit: '元',
                    imgUrl: ''
                }, {
                    goodsName: '华为',
                    categoryId: '华为',
                    status: "1",
                    description: '12',
                    price: '启用',
                    unit: '元',
                    imgUrl: ''
                }],
                Goods: {
                    goodsName: '',
                    categoryId: '',
                    status: "",
                    description: '',
                    price: '',
                    unit: '',
                    imgUrl: '',
                    id: ''
                },
                searchForm: {
                    status: "",
                    goodsName: '',
                    categoryId: '',
                },
            }
        },


    })


</script>


</body>
</html>